package structure;

import java.util.*;

public class Test9 {
    public static void main(String[] args) {
        int[] arr = {2,0,2};
    }
    //leetcode 1475.商品打折后的最终价格
    public int[] finalPrices(int[] prices) {
        Deque<Integer> deque = new ArrayDeque<>();
        int len = prices.length;
        int[] arr = new int[len];
        for(int i = 0;i < len;i++){
            int price = prices[i];
            while(!deque.isEmpty() && price <= prices[deque.peek()]){
                int index = deque.pop();
                arr[index] = prices[index] - prices[i];
            }
            deque.push(i);
            arr[i] = prices[i];
        }
        return arr;
    }
    //leetcode 739.每日温度
    public static int[] dailyTemperatures(int[] temperatures) {
        int len = temperatures.length;
        int[] arr = new int[len];
        Deque<Integer> deque = new ArrayDeque<>();
        deque.isEmpty();
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for(int i = 0;i < len;i++){
            int num = temperatures[i];
            while(!stack.isEmpty() && num > temperatures[stack.peek()]){
                int index = stack.pop();
                arr[index] = i - index;
            }
            stack.push(i);
        }
        return arr;
    }
    //leetcode 394.字符串解码
    public static String decodeString(String s) {
        int multi = 0;
        StringBuilder res = new StringBuilder();
        Stack<String> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        char[] str = s.toCharArray();
        for(int i = 0;i < str.length;i++){
            char c = str[i];
            if(c == '['){
                stack1.push(res.toString());
                stack2.push(multi);
                multi = 0;
                res = new StringBuilder();
            }else if(c == ']'){
                StringBuilder ss = new StringBuilder();
                int n = stack2.pop();
                for(int j = 0;j < n;j++){
                    ss.append(res);
                }
                res = new StringBuilder(stack1.pop() + ss);
            }else if(Character.isDigit(c)){
                multi = multi * 10 + (c - 48);
            }else {
                res.append(c);
            }
        }
        return res.toString();
    }
}
